package com.haiyi.software.app;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;

import android.R.integer;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.LayoutParams;
import android.widget.TextView;

import com.haiyi.software.app.basic.HiChatCameraBasic;
import com.haiyi.software.app.basic.HiChatImageBasic;
import com.haiyi.software.app.basic.HiChatMenuBasic;
import com.haiyi.software.app.delegate.FragmentFaceDelegate;
import com.haiyi.software.app.delegate.FragmentGifDelegate;
import com.haiyi.software.chat.HiChatAdapter;
import com.haiyi.software.entity.EntityMsg;
import com.haiyi.software.entity.EntityMsgGif;
import com.haiyi.software.entity.EntityMsgImage;
import com.haiyi.software.entity.EntityMsgText;
import com.haiyi.software.entity.EntityMsgVoice;
import com.haiyi.software.entity.HiChatMessage;
import com.haiyi.software.entity.UserFriend;
import com.haiyi.software.entity.UserInfo;
import com.haiyi.software.mycontrols.RadioButtonCenter;
import com.haiyi.software.mycontrols.SoundMeter;
import com.haiyi.software.tools.L;
import com.haiyi.software.tools.MyTools;

public class HiChat extends HiChatMenuBasic implements OnClickListener,
		FragmentFaceDelegate, FragmentGifDelegate, OnTouchListener {

	public final static int SHOW_SUB_LOCAL_IMAGE = 1;

	public final static int SHOW_SUB_GET_CAMERA = 2;

	public final static int SHOW_SUB_GET_CAMERA_EDIT = 3;

	private final static String TAG = "HiChat";

	private HiChatAdapter adapter;

	private Button btn_sendMsg;

	private EditText EditTextChatText;

	private TextView TextViewChatTitle;

	private UserFriend userFriend;

	private UserInfo myInfo;

	private Button btn_switchFace;

	private ImageView imgSwitchFace;

	private LinearLayout mFaceRoot;// 表情父容器

	private LinearLayout linearlayout_sendOther;// 其它按钮容器

	private List<RadioButtonCenter> radionList = new ArrayList<RadioButtonCenter>();

	private ListView listChat = null;

	private InputMethodManager mInputMethodManager;

	private WindowManager.LayoutParams mWindowNanagerParams;

	private boolean mIsFaceShow = false;// 是否显示表情

	private boolean mIsAddShow = false;

	private FragmentManager fragmentManager;

	private FragmentFace fragmentFace;

	private FragmentGifGrid fragmentGifGrid;

	private HorizontalScrollView horizontalScrollViewFaceBar;

	private RadioGroup radioGroup_faceBar;

	private ImageView img_chat_sendmsg;

	private boolean bool_vocie = false;

	private FrameLayout layout_chat_sendmsg;

	private FrameLayout layout_switch_voice;

	private Button btn_switch_voice;

	private ImageView img_switch_voice;

	private Button btn_switch_add, btn_chat_drop;

	private ImageView img_switch_add;

	private Button btn_chat_send_photo, btn_chat_send_img, btn_chat_send_gift;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_chat);
		MainApplication.getInstance().setHiChat(this);
		// 从主函数中获取我的个人信息
		myInfo = MainApplication.getInstance().getMineUserInfo();

		// 获取对方信息
		getParameter();

		initComp();

		initFaceBar();

		initData();

		// handlerGetDb.sendEmptyMessage(1);
		handlerGetDb.sendEmptyMessage(1);
		changeMsgReadStateToRead(userFriend.getUfID());

		setTabSelection("0");
	}

	private void initData() {
		// TODO 自动生成的方法存根
		btn_chat_drop.setTag(userFriend.getUfID());
	}

	@Override
	protected void onResume() {
		// TODO 自动生成的方法存根
		super.onResume();

	}

	protected void onPause() {

		super.onPause();

		MyTools.getInstance().playStop();

	};

	Handler handlerGetDb = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			LoadMessageFromDB(userFriend.getUfID());

			adapter = new HiChatAdapter(HiChat.this, messages, userFriend);

			listChat.setAdapter(adapter);

			listChat.setSelection(messages.size() - 1);
		};
	};

	/*
	 * 获取参数
	 */
	private void getParameter() {
		// TODO 自动生成的方法存根
		userFriend = (UserFriend) this.getIntent().getSerializableExtra(
				"com.hichat.UserFriend");
	}

	private void initFaceBar() {

		Map<String, String> faceBarMap = MainApplication.getInstance()
				.getmFaceBarMap();

		// Log.i("faceBarMap", faceBarMap.toString());

		for (Map.Entry entry : faceBarMap.entrySet()) {

			// Log.i("key", entry.getKey().toString());
			//
			// Log.i("value", entry.getValue().toString());

			RadioButtonCenter rButton = new RadioButtonCenter(this, null);

			// rButton.setWidth(40);
			// rButton.setHeight(40);
			rButton.setGravity(Gravity.CENTER);
			rButton.setText("");
			rButton.setTag(entry.getKey().toString());
			rButton.setBackgroundResource(R.drawable.selector_sexmidcenter);

			LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);

			rButton.setPadding(0, 5, 0, 5);
			rButton.setTextSize(0.0f);
			rButton.setLayoutParams(params);

			rButton.setButtonDrawable(getResources().getIdentifier(
					entry.getValue().toString(), "raw", getPackageName()));

			rButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO 自动生成的方法存根
					// int tag = Integer.valueOf(v.getTag().toString());
					setTabSelection(v.getTag().toString());
				}
			});

			radionList.add(rButton);

			radioGroup_faceBar.addView(rButton);
		}

		if (radionList.size() > 0) {
			radionList.get(0).setChecked(true);
		}

	}

	private void initComp() {
		// TODO 自动生成的方法存根

		listChat = (ListView) this.findViewById(R.id.list_chat);

		btn_sendMsg = (Button) this.findViewById(R.id.btn_chat_sendmsg);
		EditTextChatText = (EditText) this.findViewById(R.id.Editx_chat_text);
		TextViewChatTitle = (TextView) this.findViewById(R.id.txt_chat_title);
		btn_switchFace = (Button) this.findViewById(R.id.btn_switchFace);

		mFaceRoot = (LinearLayout) findViewById(R.id.face_ll);
		linearlayout_sendOther = (LinearLayout) this
				.findViewById(R.id.linearlayout_sendOther);

		imgSwitchFace = (ImageView) this.findViewById(R.id.img_switchFace);

		btn_voice = (TextView) this.findViewById(R.id.btn_voice);

		layout_chat_sendmsg = (FrameLayout) this
				.findViewById(R.id.layout_chat_sendmsg);
		layout_switch_voice = (FrameLayout) this
				.findViewById(R.id.layout_switch_voice);

		btn_switch_voice = (Button) this.findViewById(R.id.btn_switch_voice);
		img_switch_voice = (ImageView) this.findViewById(R.id.img_switch_voice);

		btn_switch_add = (Button) this.findViewById(R.id.btn_switch_add);
		img_switch_add = (ImageView) this.findViewById(R.id.img_switch_add);

		btn_chat_send_gift = (Button) this
				.findViewById(R.id.btn_chat_send_gift);
		btn_chat_send_img = (Button) this.findViewById(R.id.btn_chat_send_img);
		btn_chat_send_photo = (Button) this
				.findViewById(R.id.btn_chat_send_photo);

		btn_chat_drop = (Button) this.findViewById(R.id.btn_chat_drop);

		// 语音控件
		del_re = (LinearLayout) this.findViewById(R.id.del_re);
		volume = (ImageView) this.findViewById(R.id.volume);
		rcChat_popup = this.findViewById(R.id.rcChat_popup);
		voice_rcd_hint_rcding = (LinearLayout) this
				.findViewById(R.id.voice_rcd_hint_rcding);
		voice_rcd_hint_loading = (LinearLayout) this
				.findViewById(R.id.voice_rcd_hint_loading);
		voice_rcd_hint_tooshort = (LinearLayout) this
				.findViewById(R.id.voice_rcd_hint_tooshort);
		img1 = (ImageView) this.findViewById(R.id.img1);
		sc_img1 = (ImageView) this.findViewById(R.id.sc_img1);
		mSensor = new SoundMeter();
		// /////////////////////////////////////////////

		horizontalScrollViewFaceBar = (HorizontalScrollView) this
				.findViewById(R.id.HoriScroll_faceBar);
		radioGroup_faceBar = (RadioGroup) this
				.findViewById(R.id.radioGroup_faceBar);
		mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
		mWindowNanagerParams = getWindow().getAttributes();
		fragmentManager = getSupportFragmentManager();

		EditTextChatText.requestFocus();

		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		listChat.setOnTouchListener(this);

		EditTextChatText.addTextChangedListener(watcher);

		EditTextChatText.setOnTouchListener(this);

		EditTextChatText.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					if (mWindowNanagerParams.softInputMode == WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE
							|| mIsFaceShow) {
						mFaceRoot.setVisibility(View.GONE);
						imgSwitchFace
								.setBackgroundResource(R.drawable.chatsmile);
						mIsFaceShow = false;
						linearlayout_sendOther.setVisibility(View.GONE);
						mIsAddShow = false;
						return true;
					}
				}
				return false;
			}
		});

		TextViewChatTitle.setText("与 " + userFriend.getUfName() + " 交谈中");

		btn_sendMsg.setOnClickListener(this);
		btn_switchFace.setOnClickListener(this);
		btn_switch_voice.setOnClickListener(this);

		btn_voice.setOnLongClickListener(longClick);
		btn_voice.setOnTouchListener(touchListener);

		btn_switch_add.setOnClickListener(this);
		btn_chat_send_gift.setOnClickListener(this);
		btn_chat_send_img.setOnClickListener(this);
		btn_chat_send_photo.setOnClickListener(this);

		btn_chat_drop.setOnClickListener(this);
	}

	private TextWatcher watcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			// TODO 自动生成的方法存根

			Log.d("onTextChanged", s.length() + "|" + start + "|" + before
					+ "|" + count);

			if (s.toString().trim().length() > 0) {

				layout_switch_voice.setVisibility(View.GONE);
				layout_chat_sendmsg.setVisibility(View.VISIBLE);

			} else {
				layout_switch_voice.setVisibility(View.VISIBLE);
				layout_chat_sendmsg.setVisibility(View.GONE);
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO 自动生成的方法存根

		}

		@Override
		public void afterTextChanged(Editable s) {
			// TODO 自动生成的方法存根

		}
	};

	@Override
	public void onClick(View v) {
		// TODO 自动生成的方法存根
		switch (v.getId()) {
		case R.id.btn_switch_voice:
			if (bool_vocie) {

				btn_voice.setVisibility(View.GONE);
				btn_switchFace.setVisibility(View.VISIBLE);
				EditTextChatText.setVisibility(View.VISIBLE);
				img_switch_voice.setImageResource(R.drawable.mike);
				EditTextChatText.requestFocus();
				bool_vocie = false;
				linearlayout_sendOther.setVisibility(View.GONE);
				mIsAddShow = false;

			} else {

				mInputMethodManager.hideSoftInputFromWindow(
						EditTextChatText.getWindowToken(), 0);
				btn_voice.setVisibility(View.VISIBLE);
				btn_switchFace.setVisibility(View.GONE);
				EditTextChatText.setVisibility(View.GONE);
				img_switch_voice.setImageResource(R.drawable.keyboard);
				mFaceRoot.setVisibility(View.GONE);

				bool_vocie = true;
				imgSwitchFace.setBackgroundResource(R.drawable.chatsmile);
				mIsFaceShow = false;

				linearlayout_sendOther.setVisibility(View.GONE);
				mIsAddShow = false;
			}
			break;

		case R.id.btn_chat_sendmsg:

			String newMessage = EditTextChatText.getText().toString().trim();
			if (newMessage.length() > 0) {
				EditTextChatText.setText("");
				sendTextMsg(newMessage);

			}
			break;
		case R.id.btn_switchFace:

			if (!mIsFaceShow) {
				mInputMethodManager.hideSoftInputFromWindow(
						EditTextChatText.getWindowToken(), 0);
				mFaceRoot.setVisibility(View.VISIBLE);
				linearlayout_sendOther.setVisibility(View.GONE);
				imgSwitchFace.setBackgroundResource(R.drawable.keyboard);
				mIsFaceShow = true;
				mIsAddShow = false;
			} else {
				mFaceRoot.setVisibility(View.GONE);
				// mInputMethodManager.showSoftInput(EditTextChatText, 0);
				mInputMethodManager.hideSoftInputFromWindow(
						EditTextChatText.getWindowToken(), 0);
				imgSwitchFace.setBackgroundResource(R.drawable.chatsmile);
				mIsFaceShow = false;
				mIsAddShow = false;
			}

			break;
		case R.id.btn_switch_add:
			if (!mIsAddShow) {
				mFaceRoot.setVisibility(View.GONE);
				mInputMethodManager.hideSoftInputFromWindow(
						EditTextChatText.getWindowToken(), 0);
				imgSwitchFace.setBackgroundResource(R.drawable.chatsmile);
				linearlayout_sendOther.setVisibility(View.VISIBLE);
				mIsFaceShow = false;
				mIsAddShow = true;
			} else {
				mInputMethodManager.hideSoftInputFromWindow(
						EditTextChatText.getWindowToken(), 0);
				linearlayout_sendOther.setVisibility(View.GONE);
				mIsFaceShow = false;
				mIsAddShow = false;
			}
			break;

		case R.id.btn_chat_send_gift:
			Intent giftIntent = new Intent(this, ActivityGiftLists.class);

			giftIntent.putExtra("UID", userFriend.getUfID());

			startActivityForResult(giftIntent, 10016);
			break;

		case R.id.btn_chat_send_img:
			choseLocalImage();
			break;

		case R.id.btn_chat_send_photo:
			takeCaptureImage();
			break;

		case R.id.btn_chat_drop:
			showMenu(this, btn_chat_drop);
			break;
		}

	}

	private void sendXMPPMessage(EntityMsg entityMsg) {

		XMPPConnection connection = MainApplication.getInstance()
				.getmXMPPStream().GetXMPPConnection();

		L.i("MESSAGE", entityMsg.getPacket().toXML());

		try {

			connection.sendPacket(entityMsg.getPacket());

			L.i("conn=" + connection.getHost().toString());

		} catch (Exception e) {

			L.i("e=" + e.getMessage());
			Toast.makeText(this, "没有可连接的网络", Toast.LENGTH_LONG).show();

		}

	}

	synchronized void addNewMessage(HiChatMessage m) {
		messages.add(m);
		adapter.notifyDataSetChanged();
		listChat.setSelection(messages.size() - 1);
	}

	@Override
	protected void receiveMessageShow(Message message) {
		// TODO 自动生成的方法存根
		String uid = MyTools.getMessageHiid(message.getFrom());
		if (uid.equals(userFriend.getUfID())) {

			EntityMsg msg;
			try {
				msg = MyTools.getEntityMessageFromXML(message.toXML());

				msg.setAllMessage(message.toXML());

				msg.setDatetime(MyTools.getInstance().getDateTimeNow());

				HiChatMessage receiveMessage = new HiChatMessage(msg,
						HiChatMessage.CHATMESSAGE_PERSON);

				addNewMessage(receiveMessage);
			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}

		}
	}

	@Override
	public void ReceiveFaceEvent(SpannableString args) {
		// TODO 自动生成的方法存根
		EditTextChatText.append(args);
	}

	@Override
	public void DeleteFaceEvent() {
		// TODO 自动生成的方法存根
		int selection = EditTextChatText.getSelectionStart();
		String text = EditTextChatText.getText().toString();
		if (selection > 0) {
			String text2 = text.substring(selection - 1);
			if ("]".equals(text2)) {
				int start = text.lastIndexOf("[");
				int end = selection;
				EditTextChatText.getText().delete(start, end);
				return;
			}
			EditTextChatText.getText().delete(selection - 1, selection);
		}
	}

	private void setTabSelection(String i) {
		FragmentTransaction transaction = fragmentManager.beginTransaction();

		if (i.equals("0")) {

			fragmentFace = new FragmentFace();

			transaction.replace(R.id.FrameLayout_faceContent, fragmentFace);

		} else {

			fragmentGifGrid = new FragmentGifGrid();

			fragmentGifGrid.setGifKey(i);

			transaction.replace(R.id.FrameLayout_faceContent, fragmentGifGrid);
		}

		transaction.commit();
	}

	private void sendTextMsg(String textMsg) {

		EntityMsgText msg = new EntityMsgText();

		msg.setBody(textMsg);
		msg.setyUID(userFriend.getUfID());
		msg.setAllMessage(msg.getPacket().toXML());
		msg.setDatetime(MyTools.getInstance().getDateTimeNow());

		AddMyMessageToDB(msg, userFriend);

		HiChatMessage hiChatMessage = new HiChatMessage(msg,
				HiChatMessage.CHATMESSAGE_MINE);

		addNewMessage(hiChatMessage);

		sendXMPPMessage(msg);
	}

	@Override
	public void SendGifFaceToYou(String gifName) {
		// TODO 自动生成的方法存根
		// Toast.makeText(this, gifName, Toast.LENGTH_LONG).show();
		mFaceRoot.setVisibility(View.GONE);
		mInputMethodManager.hideSoftInputFromWindow(
				EditTextChatText.getWindowToken(), 0);
		imgSwitchFace.setBackgroundResource(R.drawable.chatsmile);
		mIsFaceShow = false;

		EntityMsgGif msg = new EntityMsgGif();

		msg.setyUID(userFriend.getUfID());
		msg.setGif(gifName);
		msg.setBody("[动态表情]");
		msg.setAllMessage(msg.getPacket().toXML());
		msg.setDatetime(MyTools.getInstance().getDateTimeNow());

		AddMyMessageToDB(msg, userFriend);
		HiChatMessage hiChatMessage = new HiChatMessage(msg,
				HiChatMessage.CHATMESSAGE_MINE);
		addNewMessage(hiChatMessage);
		sendXMPPMessage(msg);
	}

	@Override
	protected void UploadVoiceSuccess(int voicetime) {

		// TODO 自动生成的方法存根
		String str_state = jsonReturn.getAsJsonObject().get("state")
				.getAsString();
		if (str_state.equals("200")) {

			String attuid = jsonReturn.getAsJsonObject().get("body")
					.getAsJsonObject().get("attuid").getAsString();

			EntityMsgVoice msg = new EntityMsgVoice();

			msg.setyUID(userFriend.getUfID());
			msg.setBody("[语音信息]");
			msg.setVoice(attuid);
			msg.setVoice_length(voicetime);
			msg.setLocal_voice_path(voicePath);
			msg.setAllMessage(msg.getPacket().toXML());
			msg.setDatetime(MyTools.getInstance().getDateTimeNow());

			AddMyMessageToDB(msg, userFriend);

			HiChatMessage hiChatMessage = new HiChatMessage(msg,
					HiChatMessage.CHATMESSAGE_MINE);
			addNewMessage(hiChatMessage);

			msg.setLocal_voice_path("");

			sendXMPPMessage(msg);
		}

	}

	@Override
	protected void UploadImageSuccess() {

		if (jsonReturnImage.get("state").getAsString().equals("200")) {

			String attuid = jsonReturnImage.get("body").getAsJsonObject()
					.get("attuid").getAsString();

			EntityMsgImage msg = new EntityMsgImage();

			msg.setyUID(userFriend.getUfID());
			msg.setBody("[图片]");
			msg.setImage(attuid);
			msg.setLocal_image_path(imageFileSrc);
			msg.setDatetime(MyTools.getInstance().getDateTimeNow());
			msg.setAllMessage(msg.getPacket().toXML());

			AddMyMessageToDB(msg, userFriend);

			EntityMsgImage msg2 = new EntityMsgImage();

			msg2.setyUID(userFriend.getUfID());
			msg2.setBody("[图片]");
			msg2.setImage(attuid);
			msg2.setLocal_image_path("");
			msg2.setDatetime(MyTools.getInstance().getDateTimeNow());

			sendXMPPMessage(msg2);

		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO 自动生成的方法存根
		switch (v.getId()) {
		case R.id.list_chat:
			mInputMethodManager.hideSoftInputFromWindow(
					EditTextChatText.getWindowToken(), 0);
			imgSwitchFace.setBackgroundResource(R.drawable.chatsmile);
			mFaceRoot.setVisibility(View.GONE);
			mIsFaceShow = false;
			linearlayout_sendOther.setVisibility(View.GONE);
			mIsAddShow = false;
			break;
		case R.id.Editx_chat_text:
			mInputMethodManager.showSoftInput(EditTextChatText, 0);
			imgSwitchFace.setBackgroundResource(R.drawable.chatsmile);
			mFaceRoot.setVisibility(View.GONE);
			mIsFaceShow = false;
			linearlayout_sendOther.setVisibility(View.GONE);
			mIsAddShow = false;
			break;

		default:
			break;
		}
		return false;

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		switch (requestCode) {
		case SHOW_SUB_LOCAL_IMAGE:

			File f = new File(imageFileSrc);

			if (f.exists() && f.length() > 0) {

				if (createBestImage(imageFileSrc)) {

					EntityMsgImage msg = new EntityMsgImage();

					msg.setyUID(userFriend.getUfID());
					msg.setBody("[图片]");

					msg.setLocal_image_path(imageFileSrc);
					msg.setDatetime(MyTools.getInstance().getDateTimeNow());
					msg.setAllMessage(msg.getPacket().toXML());
					msg.setUserFriend(userFriend);

					HiChatMessage hiChatMessage = new HiChatMessage(msg,
							HiChatMessage.CHATMESSAGE_MINE);

					addNewMessage(hiChatMessage);

					msg.imageUpload();
				}
			}
			break;

		case SHOW_SUB_GET_CAMERA:
			cropImageUri();

			break;
		case SHOW_SUB_GET_CAMERA_EDIT:

			File fc = new File(imageFileSrc);

			if (fc.exists() && fc.length() > 0) {

				if (createBestImage(imageFileSrc)) {

					EntityMsgImage msgCAMERA = new EntityMsgImage();

					msgCAMERA.setyUID(userFriend.getUfID());
					msgCAMERA.setBody("[图片]");
					msgCAMERA.setLocal_image_path(imageFileSrc);
					msgCAMERA.setDatetime(MyTools.getInstance()
							.getDateTimeNow());
					msgCAMERA.setAllMessage(msgCAMERA.getPacket().toXML());
					msgCAMERA.setUserFriend(userFriend);

					HiChatMessage hiChatMessageCAMERA = new HiChatMessage(
							msgCAMERA, HiChatMessage.CHATMESSAGE_MINE);

					addNewMessage(hiChatMessageCAMERA);

					msgCAMERA.imageUpload();
				}
			}
			break;
		}
	}

	private Boolean createBestImage(String imageFileSrc) {

		Bitmap bitmap = BitmapFactory.decodeFile(imageFileSrc);

		int imgWitdh = bitmap.getWidth();
		int imgHeight = bitmap.getHeight();

		int imgOutWidth = 0;
		int imgOutHeigth = 0;

		if (imgWitdh > imgHeight) {

			imgOutWidth = 1280;
			imgOutHeigth = (int) ((imgHeight * 1.0 / imgWitdh) * imgOutWidth);

		} else {
			imgOutHeigth = 1280;
			imgOutWidth = (int) ((imgWitdh * 1.0 / imgHeight) * imgOutHeigth);
		}

		Bitmap bitmapNew = Bitmap.createScaledBitmap(bitmap, imgOutWidth,
				imgOutHeigth, true);

		FileOutputStream out;

		try {

			out = new FileOutputStream(imageFileSrc);

			bitmapNew.compress(Bitmap.CompressFormat.JPEG, 90, out);

			out.flush();

			out.close();

			return true;

		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();

			return false;
		}
	}
}
